1. Previous Important algorithms IA
- N-gram Models - 1950s: Predict the next word based on the previous ones (e.g., last 2 words in a bigram).
- Bag of Words - 1950s: A simple model that represents text as a set of words without considering grammar or word order.
- Cosine Similarity - 1958: Measures how similar two vectors are by the angle between them.
Formula
Where:
- is the first vector.
- is the second vector.
- is the dot product of vectors A and B.
- is the magnitude (norm) of vector A.
- is the magnitude (norm) of vector B.
- TF-IDF (Term Frequency-Inverse Document Frequency) - 1970s: Weighs words based on their frequency in a document and their rarity in the corpus, helping to identify important terms.
- Word Embeddings - 2013: Convert words into vectors that capture their meaning and relationships.
2. Recurrent Neural Network RNN
It is designed to process sequential data. RNNs maintain a hidden state that is updated at each time step, allowing the model to remember information from previous inputs. The basic equation is:
Where:
- : The hidden state at time .
- : The input at time .
- : The weight matrix connecting the input () to the hidden state ().
- : The weight matrix connecting the previous hidden state () to the current hidden state ().
- : The bias term added to the weighted sum.
- activation: An activation function, such as tanh, sigmoid, or ReLU, applied to the sum.
RNNs form the foundation for many modern AI applications by enabling the processing of sequences with context. But they have limitations in remembering long-term information due to the vanishing gradient.
3. Long Short-Term Memory (LSTM)
Designed to improve memory from an RNN, it decides what to remember using 3 gates (forget, input, and output), maintains a cell state for long-term memory, and updates its state using a candidate cell and a hidden value. This improvements make this achievement more slowly
Where:
- is the input at time step
- is the previous hidden state
- is the combined weight matrix for all gates and candidate
- is the bias vector
- is the sigmoid activation function
- is the hyperbolic tangent activation
- is the forget gate
- is the input gate
- is the output gate
- is the candidate cell state
- is the current cell state
- is the previous cell state
- is the current hidden state
- represents element-wise (Hadamard) multiplication
Forget Gate
The forget gate decides which information from the previous cell state should be discarded. It is calculated as:
Where:
- : The forget gate output at time .
- : Weight matrix for the forget gate.
- : The hidden state at the previous time step.
- : The input at time .
- : The bias term for the forget gate.
- : The sigmoid activation function.
Input Gate
The input gate decides which values from the current input and the previous hidden state will update the cell state. It is calculated as:
Where:
- : The input gate output at time .
- : Weight matrix for the input gate.
- : The previous hidden state.
- : The current input.
- : The bias term for the input gate.
- : The sigmoid activation function.
Output Gate
The output gate decides what the next hidden state will be, based on the current cell state. It is calculated as:
Where:
- : The output gate output at time .
- : Weight matrix for the output gate.
- : The previous hidden state.
- : The current input.
- : The bias term for the output gate.
- : The sigmoid activation function.
Candidate Cell State
Where:
- is the candidate cell state at time step .
- is the weight matrix for the candidate cell state.
- is the previous hidden state.
- is the current input.
- is the bias term for the candidate cell state.
Cell State Update
Where:
- is the current cell state at time step .
- is the forget gate’s output at time step .
- is the previous cell state.
- is the input gate’s output at time step .
- is the candidate cell state.
Hidden State Update
Where:
- is the hidden state at time step .
- is the output gate’s output at time step .
- is the current cell state at time step .
4. Gated Recurrent Unit (GRU)
Designed to simplify the LSTM by using only an update gate and a reset gate, making it more efficient. It merges cell and hidden states into one, reducing complexity while keeping strong performance.
Where:
- is the input at time t.
- is the previous hidden state.
- is the update gate (controls how much of the past to keep).
- is the reset gate (controls how much past info to forget).
- is the candidate hidden state.
- is the new hidden state.
- is element-wise multiplication.
- , , are weights and biases.
- is the sigmoid function.
Then, there were other improvements, like processing the sequence of data from right to left and in both directions. Finally, a scoring mechanism was introduced to compare the last element with the sequence and understand the importance of each word. This improved the handling of sequence dependencies, addressing the memory problem (which is the basis of transformers).
5. Transformers
- Input: a sequence of words (tokens)
- Embedding: transform the words into numerical representations, using methods like Word2Vec or learned embeddings
- Positional Encoding: adds additional information to help the model understand the order of the words
- Attention Mechanism: calculates the importance of each word with respect to the others. The calculation is done in parallel — for every word, its attention to other words is computed. So, if there are two similar words with different meanings, they will have different attention values (this is why transformers can understand context and meaning)
Where:
Q
is the query (the current word being processed)K
is the key (represents all words)V
is the value (information of all words)d_k
is the dimension of the key vectors
- Feedforward: a small layer that adjusts each vector (refines the representation), applied to each word independently
- Normalization: the result is normalized to avoid issues like vanishing gradients
- Decoder: after several iterations, the attention results are transformed back into words
How is this used?
After training, the model holds a matrix of attention scores. You input a sentence, and based on these learned weights, the model predicts the next word step by step.